package com.controller.manager;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.service.UserService;

@Controller
@RequestMapping("/")
public class UserController {

	@Resource
	private UserService userService;

	@RequestMapping(value = "/toUserPage", method = RequestMethod.GET)
	private ModelAndView toUserPage(HttpServletRequest request) {
		String username = (String) request.getSession().getAttribute("username");
		if (username == null || "".equals(username)) {
			return new ModelAndView("/login");
		}
		return new ModelAndView("/manager/user");
	}

	@RequestMapping(value = "/addUserInfo", method = RequestMethod.POST)
	@ResponseBody
	public String addUserInfo(@RequestParam Map<String, String> paramsMap) {
		int result = userService.addUserInfo(paramsMap);
		if (result >= 0) {
			return "success";
		} else if (result == -2) {
			return "has";
		}
		return "error";
	}

	@RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, String>> getUserInfo(@RequestParam Map<String, String> paramsMap) {
		List<Map<String, String>> list = userService.getUserInfo(paramsMap);
		return list;
	}

	@RequestMapping(value = "/deleteUserInfo", method = RequestMethod.POST)
	@ResponseBody
	public String deleteUserInfo(@RequestParam Map<String, String> paramsMap) {
		int result = userService.deleteUserInfo(paramsMap);
		if (result >= 0) {
			return "success";
		}
		return "error";
	}

	@RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST)
	@ResponseBody
	public String updateUserInfo(@RequestParam Map<String, String> paramsMap) {
		int result = userService.updateUserInfo(paramsMap);
		if (result >= 0) {
			return "success";
		}
		return "error";
	}
}
